<template>
  <span v-if="this.fileId">
    <a-button v-if="type == 'button'" icon="share-alt" @click="handleSendShareYami()">分享文件 </a-button>
    <span v-else-if="type == 'text'" style="padding:5px;10px;cursor:pointer" @click="handleSendShareYami()"
      ><a-icon type="share-alt" />分享</span
    >
    <a-icon
      v-else-if="type == 'icon'"
      type="share-alt"
      style="padding:5px;10px;cursor:pointer"
      @click="handleSendShareYami()"
    />
    <user-select ref="userSelect" :limit="100" @setResult="setResult" />
  </span>
</template>
<script>
import { postAction, getAction } from '@/api/manage'
export default {
  name: 'FileShare',
  components: {
    UserSelect: () => import('@/components/tools/UserSelect'),
  },
  props: {
    fileId: {
      type: String,
      default: null,
    },
    type: {
      type: String, //button,text,icon
      default: 'button',
    },
  },
  data() {
    return {
      url: {
        sendShareYami: '/lib/hteLibFileRange/sendShareYami',
      },
    }
  },
  methods: {
    handleSendShareYami() {
      this.$refs.userSelect.doSelect()
    },
    setResult(result) {
      if (result.length > 100) {
        this.$message.warning('单次分享人员不能超过100个', 2)
        return
      }
      var usernames = []
      var realnames = []
      if (result != null) {
        result.forEach((item) => {
          usernames.push(item.username)
          realnames.push(item.realname)
        })
      }
      let that = this
      this.$confirm({
        title: '确认分享',
        content: '请确认将文件分享给选中的人员?',
        onOk: function () {
          postAction(that.url.sendShareYami + '?fileId=' + that.fileId, usernames).then((res) => {
            if (res.success) {
              that.$message.success('分享成功')
            } else {
              that.$message.warning('分享失败：' + e.message)
            }
          })
        },
      })
    },
  },
}
</script>
